package net.hzlj.platform.enterprise.receiver;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.activity.DeviceAuthorizeActivity;
import net.hzlj.platform.enterprise.common.Constant;
import net.hzlj.platform.enterprise.utils.AppPreferencesUtils;
import net.hzlj.platform.enterprise.utils.PreferencesUtils;


/****
 * 用于显示设备激活窗口
 */
public class DeviceReceiver extends android.app.admin.DeviceAdminReceiver {

    private static final String TAG = DeviceReceiver.class.getSimpleName();

    /***
     * 获取设备存储的数值
     * @param context
     * @return
     */
    public static SharedPreferences getDevicePreference(Context context) {
        return context.getSharedPreferences(
                android.app.admin.DeviceAdminReceiver.class.getName(), 0);
    }

    private void showToast(Context context, CharSequence text) {
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onEnabled(Context context, Intent intent) {
        Log.i(TAG, "----onEnabled----");
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        Log.i(TAG, "----onDisabled----");
        boolean flag = AppPreferencesUtils.getBoolean(BootApp.getAppContext(), PreferencesUtils.KEY_ALLOW_UNINSTALLATION, Constant.Socket.DEFAULT_ALLOW_UNINSTALL);
        Log.i(TAG, "----flag----" + flag);
        if (!flag) {
            Intent intentAdmin = new Intent(context, DeviceAuthorizeActivity.class);
            intentAdmin.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            context.startActivity(intentAdmin);
        }
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        // 这里处理 不可编辑设备。
        return "请不要取消，取消后将会继续提示授权，否则设备不可用！";
    }
}
